package avicit.bdp.dcs.stream.rest;

import avicit.bdp.dcs.stream.dto.MeasureParaDto;
import avicit.bdp.dcs.stream.dto.MsgTemplateDto;
import avicit.bdp.dcs.stream.service.MsgTemplateService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2020-10-16
 * @类说明：MsgTemplateRest
 * @修改记录：
 * @注意事项：
 * @主要功能：消息模板Rest接口，包括消息模板、模板参数、模板文件处理功能
 */
@RestController
@RequestMapping("/api/bdp/dcs/stream/template")
@Api(tags = "消息模板管理接口")
public class MsgTemplateRest {
    private static final Logger logger = LoggerFactory.getLogger(MsgTemplateRest.class);

    @Autowired
    private MsgTemplateService msgTemplateService;

    /**
     * 新建消息模板
     * @param msgTemplateDto
     * @return
     */
    @PostMapping("/createMsgTemplate")
    @ApiOperation(value = "新增模板")
    public ResponseMsg createMsgTemplate(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        this.msgTemplateService.createMsgTemplate(msgTemplateDto);
        return responseMsg;
    }

    /**
     * 修改消息模板
     * @param msgTemplateDto
     * @return
     */
    @PostMapping("/updateMsgTemplate")
    @ApiOperation(value = "修改模板")
    public ResponseMsg<String> updateMsgTemplate(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        this.msgTemplateService.updateMsgTemplate(msgTemplateDto);
        return responseMsg;
    }

    /**
     * 删除消息模板
     * @param msgTemplateDto
     * @return
     */
    @DeleteMapping("/deleteMsgTemplate")
    @ApiOperation(value = "删除模板")
    public ResponseMsg<Boolean> deleteMsgTemplate(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        this.msgTemplateService.deleteMsgTemplateByIds(msgTemplateDto);

        return responseMsg;
    }

    /**
     * 分页查询/模糊查询消息模板
     * @param sort
     * @param deviceName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getMsgTemplateList")
    @ApiOperation(value = "查询模板列表")
    public ResponseMsg<QueryRespBean<MsgTemplateDto>> getMsgTemplateList(
            @ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按名称升序：3，按名称倒序：4", name = "sort") @RequestParam(required = false) Integer sort,
            @ApiParam(value = "模板名称", name = "deviceName") @RequestParam(required = false) String deviceName,
            @ApiParam(value = "空间ID", name = "projectId") @RequestParam(required = false) String projectId,
            @ApiParam(value = "当前页", name = "pageNo", required = true)  @RequestParam(defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页显示条数", name = "pageSize", required = true) @RequestParam(defaultValue = "10") Integer pageSize) {
        ResponseMsg<QueryRespBean<MsgTemplateDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.msgTemplateService.getMsgTemplateList(sort, deviceName, projectId, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 查询消息模板
     * @return
     */
    @GetMapping("/getMsgTemplateListAll")
    @ApiOperation(value = "查询模板列表")
    public ResponseMsg<List<MsgTemplateDto>> getMsgTemplateList(
            @ApiParam(value = "模板名称", name = "deviceName") @RequestParam(required = false) String deviceName,
            @ApiParam(value = "空间ID", name = "projectId") @RequestParam(required = false) String projectId) {
        ResponseMsg<List<MsgTemplateDto>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(this.msgTemplateService.getMsgTemplateList(deviceName, projectId));
        return responseMsg;
    }

    /**
     * 添加参数
     * @param msgTemplateDto
     * @return
     */
    @PostMapping("/addMsgTemplateParas")
    @ApiOperation(value = "新增模板测量参数")
    public ResponseMsg addMsgTemplateParas(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        this.msgTemplateService.addMsgTemplateParas(msgTemplateDto);
        return responseMsg;
    }

    /**
     * 更新参数
     * @param msgTemplateDto
     * @return
     */
    @PostMapping("/updateMsgTemplateParas")
    @ApiOperation(value = "更新模板测量参数")
    public ResponseMsg updateMsgTemplateParas(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.msgTemplateService.updateMsgTemplateParas(msgTemplateDto);

        return responseMsg;
    }

    /**
     * 查询参数
     * @param id
     * @return
     */
    @GetMapping("/getMsgTemplateParas")
    @ApiOperation(value = "查询模板测量参数列表")
    public ResponseMsg<List<MeasureParaDto>> getParasByTemplateId(
            @ApiParam(value = "模板主键ID", name = "id") @RequestParam(required = true) String id,
            @ApiParam(value = "参数名称", name = "paraName") @RequestParam(required = false) String paraName) {
        ResponseMsg<List<MeasureParaDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.msgTemplateService.getParasByTemplateId(id, paraName));

        return responseMsg;
    }

    /**
     * 删除参数
     * @param msgTemplateDto
     * @return
     */
    @DeleteMapping("/deleteMsgTemplateParas")
    @ApiOperation(value = "删除模板测量参数")
    public ResponseMsg<Boolean> deleteMsgTemplateParas(@RequestBody MsgTemplateDto msgTemplateDto) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        this.msgTemplateService.deleteMsgTemplateParas(msgTemplateDto);
        return responseMsg;
    }

    /**
     * 导入模板文件
     * @param msgTemplateDto
     * @return 返回解析结果，用于VUE前端展示
     * @throws Exception
     */
    @PostMapping("/upload")
    @ApiOperation(value = "导入模板文件")
    public ResponseMsg<List<MsgTemplateDto>> upload(@ApiParam(value = "templateDTO", name = "templateDTO")
                                                            MsgTemplateDto msgTemplateDto) throws Exception{
        ResponseMsg<List<MsgTemplateDto>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.msgTemplateService.upload(msgTemplateDto));

        return responseMsg;
    }

    /**
     * 下载模板示例文件。
     * 注意：不是下载用户导入的模板文件，不提供此功能；
     * @param filePath
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadFile")
    @ApiOperation(value = "下载文件")
    public void downloadFile(@ApiParam(value = "文件路径", name = "filePath", required = true) @RequestParam String filePath
            , HttpServletResponse response) {
        this.msgTemplateService.downloadFile(filePath, response);
    }

}
